package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.RoleDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.dao.RoleDao;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author Xutao Bai
 * @Date created in 2020/1/3
 * @description
 * @Version 1.0
 */
@Service
@Slf4j
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;
    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        log.info("[角色-分页]data:{}",queryPageBean);
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Role> page = roleDao.selectByCondition(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Transactional
    @Override
    public void add(Role role, Integer[] permissions, Integer[] menus) {
        log.info("[角色-添加]data:{},permissions:{},menus:{}",role,permissions,menus);
        //插入基本数据
        roleDao.insert(role);
        //逐条出入权限数据
        for (Integer permissionId : permissions) {
            roleDao.insertRoleAndPermission(role.getId(),permissionId);
        }
        //逐条出入菜单数据
        for (Integer menuId : menus) {
            roleDao.insertRoleAndMenu(role.getId(),menuId);
        }
    }

    @Override
    public Role findById(Integer id) {
        log.info("[角色-编辑回显]id:{}",id);
        return roleDao.selectById(id);
    }

    @Override
    public List<Integer> findPermissionsByRoleId(Integer id) {
        log.info("[角色-编辑权限id回显]id:{}", id);
        return roleDao.selectPermissionsByRoleId(id);
    }

    @Override
    public List<Integer> findMenusByRoleId(Integer id) {
        log.info("[角色-编辑菜单id回显]id:{}", id);
        return roleDao.selectMenusByRoleId(id);
    }

    @Transactional
    @Override
    public void edit(Role role, Integer[] permissions, Integer[] menus) {
        log.info("[角色-编辑]data:{},permissions:{},menus:{}", role, permissions, menus);
        //更新角色信息
        roleDao.update(role);
        //更新权限关联
        roleDao.deleteRoleAndPermissionByRoleId(role.getId());
        for (Integer permissionId : permissions) {
            roleDao.insertRoleAndPermission(role.getId(),permissionId);
        }
        //更新菜单关联
        roleDao.deleteRoleAndMenuByRoleId(role.getId());
        for (Integer menuId : menus) {
            roleDao.insertRoleAndMenu(role.getId(),menuId);
        }
    }

    @Transactional
    @Override
    public void delete(Integer id) {
        //删除关联信息
        roleDao.deleteRoleAndPermissionByRoleId(id);
        roleDao.deleteRoleAndMenuByRoleId(id);
        //删除角色信息
        roleDao.delete(id);
    }

    /**
     * 查询所有
     *
     * @return
     */
    @Override
    public List<Role> findAll() {
        return roleDao.selectAll();

    }

    /**
     * 根据用户id查询角色id
     *
     * @param userId
     * @return
     */
    @Override
    public int selectByUserId(int userId) {
        return roleDao.selectRoleId(userId);
    }
}
